<!DOCTYPE html>
<html lang="">

<head>
  <meta charset="UTF-8">
  <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML Module Page</title>
</head>

<body>
  <script>
    const loadPage = async (elem, htmlFile) => {
      try {
        const response = await fetch(htmlFile)
        if (!response.ok) {
          throw new Error(`HTTP ${response.status}: ${response.statusText}`)
        }
        const data = await response.text()

        const parser = new DOMParser()
        const doc = parser.parseFromString(data, 'text/html')

        const elementsToAdd = []
        const processElements = async (elements, canAddNormalElements) => {
          for (const element of Array.from(elements)) {
             if (element instanceof HTMLScriptElement) {
               if (element.type == "module") continue

               let newElement = document.createElement('script')
               if (element.src) {
                 newElement.src = element.src
                 if (element.type) newElement.type = element.type

                 await new Promise((resolve, reject) => {
                   newElement.onload = resolve
                   newElement.onerror = reject
                   document.body.appendChild(newElement)
                 })
               } else {
                 newElement.textContent = element.textContent
                 if (element.type) newElement.type = element.type
                 document.body.appendChild(newElement)
               }
             }
            else if (element instanceof HTMLLinkElement) {
              if (!element.href) continue

              let newElement = document.createElement('link')
              newElement.href = element.href
              if (element.rel) newElement.rel = element.rel
              if (element.type) newElement.type = element.type

              await new Promise((resolve, reject) => {
                newElement.onload = resolve
                newElement.onerror = reject
                document.head.appendChild(newElement)
              })
            }
            else if (canAddNormalElements && element instanceof HTMLElement) {
              elementsToAdd.push(element)
            }
          }
        }

        await processElements(doc.head.children, false)
        await processElements(doc.body.children, true)

        elementsToAdd.forEach(element => elem.appendChild(element))

      }
      catch (error) { }
    }

    const loadBridgeClient = async () => {
      const response = await fetch('js/bi-bridge-client.js')
      const data = await response.text()
      const script = document.createElement('script')
      script.textContent = data
      document.body.appendChild(script)
    }

    document.addEventListener('DOMContentLoaded', async () => {

      const urlParams = new URLSearchParams(window.location.search)
      const entryName = urlParams.get('entryName')
      const bridgeId = urlParams.get('bridgeId')
      if (!entryName || !bridgeId) return;

      try {
        const htmlFile = `${entryName}.main.html`
        await loadPage(document.body, htmlFile)
        await loadBridgeClient()
      }
      catch (error) {
        return
      }

      biInitBridgeClient(bridgeId)
    })
  </script>
</body>

</html>